package com.mumu.android.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


/**
 * @Author: MuMu
 * @Date: 2025/3/8 10:43
 */
public class MuMuTcpClient {
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private final String serverIp;
    private final int serverPort;
    private final TcpClientListener listener;
    private boolean isRunning = false;

    public interface TcpClientListener {
        void onMessageReceived(String message);
        void onMessageSend(String message);
        void onConnectionStatusChanged(boolean isConnected);
        void onConnectionError(String errorMessage);
    }

    public MuMuTcpClient(String serverIp, int serverPort, TcpClientListener listener) {
        this.serverIp = serverIp;
        this.serverPort = serverPort;
        this.listener = listener;
    }

    public void connect() {
        new Thread(() -> {
            try {
                socket = new Socket(serverIp, serverPort);

                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream(), "GB2312");
                out = new PrintWriter(outputStreamWriter, true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GB2312"));
                isRunning = true;
                listener.onConnectionStatusChanged(true);

                // 持续接收消息
                String message;
                while (isRunning && (message = in.readLine()) != null) {
                    listener.onMessageReceived(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
                listener.onConnectionError("Connection error: " + e.getMessage()); // 触发错误回调
            } finally {
                disconnect();
            }
        }).start();
    }

    public void sendMessage(String message) {
        if (out != null && !out.checkError()) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    out.println(message);
                    listener.onMessageSend(message);
                }
            }).start();
        } else {
            listener.onConnectionStatusChanged(false);
        }
    }

    public void disconnect() {
        isRunning = false;
        try {
            if (socket != null) {
                socket.close();
            }
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
            listener.onConnectionStatusChanged(false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getLocalAddress(){
        return socket == null ? "--" : socket.getLocalAddress().getHostAddress();
    }

    public int getLocalPort(){
        return socket == null ? 0 : socket.getLocalPort();
    }
}